package com.aos.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.annotation.Resource;

/**
 * Created by fangzhipeng on 2017/5/27.
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter{
    Logger log = LoggerFactory.getLogger(ResourceServerConfiguration.class);

//    @Autowired
//    TokenStore tokenStore;

    /**Redis配置*/
    @Autowired
    private RedisConnectionFactory connectionFactory;

    //内存存储
//    private TokenStore tokenStore = new InMemoryTokenStore();

    //数据库存储
    //JdbcTokenStore tokenStore=new JdbcTokenStore(dataSource);

    /**Redis存储*/
//    @Bean
//    public TokenStore tokenStore() {
//        RedisTokenStore redis = new RedisTokenStore(connectionFactory);
//        return redis;
//    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
//                .regexMatchers(".*swagger.*",".*v2.*",".*webjars.*","/user/login.*",
//                        "/user/registry.*","/user/test.*","/bus/refresh.*","/base/getCaptcha.*","/synchro/data").permitAll()
//                .antMatchers("/**").authenticated();
        .antMatchers("/**").permitAll();

//        http.authorizeRequests()
//                //.authorizeRequests().anyRequest().authenticated()
//                .antMatchers("/login/v1/login*","/user/v1/register","**.js","**.css","**.html",
//                        "**swagger**","/permission/**").permitAll()
//                .anyRequest().authenticated();
//        http.csrf().disable();
    }


//    @Override
//    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
//        log.info("Configuring ResourceServerSecurityConfigurer ");
//        resources.resourceId("uaa-service-gov").tokenStore(tokenStore());
//    }

}
